﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Benefit.Models.Sys;
using System.Data.Entity;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 账户资金情况
    /// </summary>
    public class ServiceAccountDayMoney : Benefit.Interface.Day.IAccountDayMoney
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountDayMoney(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.AccountDayMoney where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.AccountDayMoney c in query)
            {
                db.AccountDayMoney.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存账号日资金情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitDayAccountDayMoney(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account itaccount = new ServerData.ServiceT_Account(db);
            Interface.ServerData.IT_SettlementMoney itsm = new ServerData.ServiceT_SettlementMoney(db);
            Interface.Sys.IAccount ia = new Sys.ServiceAccount(db);
            List<Models.ServerData.T_Account> serverAccounts = itaccount.GetList();
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {
                Models.Day.AccountDayMoney change = new Models.Day.AccountDayMoney();

                //Sys.Account _account = new Sys.Account();
                change.AccountId = ia.GetAccountId(account.LoginAccount);
   
                Models.ServerData.T_SettlementMoney money = itsm.GetSettlementMoney(account.Id, pdate);
                change.AppendMargin = money.AppendMargin;
                change.AvailableMoney = money.AvailableMoney;
                change.BaseMargin = money.BaseMargin;
                change.CloseProfit = money.CloseProfit;
                change.Commission = money.Commission;
                change.Datetime = money.Datetime;
                change.DeliveryCommission = money.DeliveryCommission;
                change.DeliveryMargin = money.DeliveryMargin;
                change.Deposit = money.Deposit;
                change.DynamicMoney = money.DynamicMoney;
                change.FrozenCommission = money.FrozenCommission;
                change.FrozenMargin = money.FrozenMargin;
                change.LongPosition = money.LongPosition;
                change.Margin = money.Margin;
                change.Mortgage = money.Mortgage;
                change.PositionProfit = money.PositionProfit;
                change.Premortgage = money.Premortgage;
                change.PresetTlePosit = money.PresetTlePosit;
                change.RiskRate = money.RiskRate;
                change.SettlePosit = money.SettlePosit;
                change.ShortPosition = money.ShortPosition;
                change.TradeHistoryId = tradeHistoryId;
                change.WithDraw = money.WithDraw;
                change.WithDrawQuota = money.WithDrawQuota;
                db.AccountDayMoney.Add(change);
            }
            db.SaveChanges();
        }


        public Models.Day.AccountDayMoney GetAccountDayMoney(int tradeHistoryId, int accountid)
        {
            Models.Day.AccountDayMoney returnValue = null;
            var query = db.AccountDayMoney.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId).Include(a => a.Account);
            if (query.Count() > 0)
            {
                returnValue = query.First();
            }
            return returnValue;
        }
    }
}